package cn.edu.ctbu.ajax514.restcontroller;

import cn.edu.ctbu.ajax514.domain.Student;
import cn.edu.ctbu.ajax514.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping(value = "/webapi/student")
public class StudentRestController {
    @Autowired
    StudentService studentService;
    /**
     * 查询全部学生信息
     * @return
     */
    @GetMapping("/list")
    public List<Student> GetAll(){
        return studentService.findAll();
    }
    /**
     * 按id获取学生信息
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
        Student student = studentService.findById(id);
        return student;
    }
    /**
     * 查询学生信息的3种方式
     * @param searchBy 后面根据用户选择的方式对应serachByid/no/name
     * @param keyword 关键词id/no/name
     * @return
     */
    @GetMapping("/search")
    public ResponseEntity<?> searchStudents(@RequestParam String searchBy, @RequestParam String keyword) {
        switch (searchBy) {
            case "id":
                // 根据ID查询
                Integer id = Integer.parseInt(keyword);
                Student studentById = studentService.findById(id);
                if (studentById != null) {
                    return ResponseEntity.ok(Collections.singletonList(studentById));
                }
                break;
            case "no":
                // 根据学号查询
                Integer no = Integer.parseInt(keyword);
                Student studentByNo = studentService.findByNo(no);
                if (studentByNo != null) {
                    return ResponseEntity.ok(Collections.singletonList(studentByNo));
                }
                break;
            case "name":
                // 根据姓名查询
                List<Student> studentsByName = studentService.searchByName(keyword);
                if (!studentsByName.isEmpty()) {
                    return ResponseEntity.ok(studentsByName);
                } else {
                    return ResponseEntity.notFound().build(); // 返回404状态码表示未找到
                }
            default:
                // 其他情况
                // 可以返回空列表或者其他逻辑
                break;
        }
        // 如果没有找到匹配的学生信息，返回空响应
        return ResponseEntity.noContent().build();
    }
    /**
     * 新增学生
     * @param student
     * @return
     */
    @PostMapping(value = "/insert")
    public Student insert(Student student){
        Student student1 = studentService.insert(student);
        return student1;
    }
    /**
     * 更新学生
     * @param student
     * @return
     */
    @PutMapping(value = "/update")
    public Student update(Student student){
        Student student1 = studentService.update(student);
        return student1;
    }
    /**
     * 按id删除学生
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        studentService.deleteById(id);
    }
    /**
     * 按课程号查询选课学生信息
     * @param cno
     * @return
     */
    @GetMapping("/cno/{cno}")
    public List<Student> getByCourse(@PathVariable Integer cno) {
        return studentService.findByCourse(cno);
    }
    /**
     * 检查学号是否已存在
     * @param no 学号
     * @return ResponseEntity 包含检查结果的响应实体
     */
    @GetMapping("/checkNo/{no}")
    public ResponseEntity<?> checkStudentNo(@PathVariable Integer no) {
        // 调用服务层方法检查学号是否已存在
        boolean exists = studentService.checkStudentNoExists(no);
        if (exists) {
            // 如果学号已存在，则返回自定义的响应体，表示学号重复
            return ResponseEntity.ok().body("学号已存在，请重新指定唯一学号！");
        } else {
            // 如果学号不存在，则返回状态码200（OK）
            return ResponseEntity.ok().build();
        }
    }
}
